package mc933.proj.clickposition;

import mc933.proj.clickposition.RestClient.RequestMethod;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.Toast;

public class ClickPositionActivity extends Activity {
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		LocationManager locationMgr = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
		locationMgr.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0,
				new GPSLocationListener(this));

		final ImageButton RedButton = (ImageButton) findViewById(R.id.btn_save_point);
		RedButton.setOnTouchListener(new OnTouchListener() {

			public boolean onTouch(View v, MotionEvent event) {
				if (event.getAction() == MotionEvent.ACTION_DOWN) {
					RedButton.setImageResource(R.drawable.pressed);
				} else if (event.getAction() == MotionEvent.ACTION_UP) {
					LocationManager LM = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

					if (LM.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
						Location lastLocation = LM
								.getLastKnownLocation(LocationManager.GPS_PROVIDER);
						if (lastLocation != null) {
							Double latitude = lastLocation.getLatitude();
							Double longitude = lastLocation.getLongitude();
							
							RestClient restClient = new RestClient(
									"http://mc933.lab.ic.unicamp.br:8009/coordenada/inserir");
							restClient.AddParam("lati", latitude.toString());
							restClient.AddParam("long", longitude.toString());

							Toast.makeText(RedButton.getContext(), "Sua localizacao foi enviada", Toast.LENGTH_SHORT).show();
							try {
								restClient.Execute(RequestMethod.GET);
							} catch (Exception e) {
								Toast.makeText(RedButton.getContext(), "Erro ao enviar localizacao", Toast.LENGTH_SHORT).show();
							}
							
						}
						else {
							Toast.makeText(RedButton.getContext(), "Nenhuma coordenada pode ser enviada", Toast.LENGTH_SHORT).show();
						}
					} else {
						Toast.makeText(RedButton.getContext(), "Ative o GPS para enviar coordenadas", Toast.LENGTH_SHORT).show();
					}
					RedButton.setImageResource(R.drawable.unpressed);
				}

				return false;
			}
		});

		final Button ButtonShowList = (Button) findViewById(R.id.btn_show_list);
		ButtonShowList.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				RestClient restClient = new RestClient(
						"http://mc933.lab.ic.unicamp.br:8009/coordenada/lista");
				try {
					restClient.Execute(RequestMethod.GET);
				} catch (Exception e) {
					Toast.makeText(RedButton.getContext(), "Erro ao obter lista de pontos", Toast.LENGTH_SHORT).show();				}
				
				Intent intent = new Intent();
		        intent.setClass(getApplicationContext(), ListPoints.class);
				intent.putExtra("listPoints", restClient.getResponse());
		        startActivity(intent);
			}
		});
	}
}